﻿using System;
using System.Diagnostics;
using System.Text;

namespace Utilities
{
    /// <summary>
    /// Wrapper around the TraceSource object
    /// </summary>
    public static class TraceSourceExtensions
    {
        public static void LogVerbose(this TraceSource traceSource, string format, params object[] args)
        {
            traceSource.TraceEvent(TraceEventType.Verbose, 0, format, args);
        }

        public static void LogInformation(this TraceSource traceSource, string format, params object[] args)
        {
            traceSource.TraceInformation(format, args);
        }

        public static void LogError(this TraceSource traceSource, Exception exception)
        {
            exception = exception.GetInnermostException();

            StringBuilder builder = new StringBuilder();
            builder.Append(exception.Message);
            builder.AppendLine();
            builder.Append(exception.StackTrace);

            LogError(traceSource, builder.ToString());
        }

        public static void LogError(this TraceSource traceSource, string message)
        {
            traceSource.TraceEvent(TraceEventType.Error, 0, message);
        }

        public static void LogError(this TraceSource traceSource, string format, params object[] args)
        {
            traceSource.TraceEvent(TraceEventType.Error, 0, string.Format(format, args));
        }

        public static void LogWarning(this TraceSource traceSource, string format, params object[] args)
        {
            traceSource.TraceEvent(TraceEventType.Warning, 0, format, args);
        }
    }
}